﻿Ext.Loader.setConfig({ enabled: true });
Ext.Loader.setPath('Ext.ux', '../../js/ext/ux/statusbar');
Ext.require([
    'Ext.ux.StatusBar'
]);
Ext.BLANK_IMAGE_URL = '/css/Images/s.gif';
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';
var IsLogining = false;
function Main_Login() {
    var logoPanel = new Ext.Panel({
        baseCls: 'x-plain',
        id: 'login-logo',
        region: 'center'
    });


    var checkcode = Ext.create('Ext.ux.CheckCode', {
        cls: 'key',
        fieldLabel: '验证码',
        name: 'CheckCode',
        id: 'CheckCode',
        allowBlank: false, labelAlign: 'right',
        isLoader: true,
        blankText: '验证码不能为空',
        codeUrl: '/manager/user/GetCheckCode',
        width: 200
    })

    var loginForm = Ext.create("Ext.form.Panel", {
        id: 'loginForm',
        region: 'south',
        baseCls: 'x-plain',
        border: false,
        bodyPadding: 10,
        buttonAlign: "center",
        fieldDefaults: {
            labelAlign: 'left',
            labelWidth: 160,
            labelStyle: 'font-weight:bold'
        },
        defaults: {
            margins: '0 0 10 0', msgTarget: "side", labelAlign: 'right'
        },
        items: [{
            xtype: 'textfield',
            fieldLabel: '登录名',
            name: 'username',
            cls: 'username',
            blankText: '登录名不能为空',
            regex: /^[\da-zA-Z_]{1,20}$/,
            regexText: "请填写1～20个数字或字母",
            validateOnBlur: false,
            allowBlank: false
        }, {
            xtype: 'textfield',
            inputType: 'password',
            name: 'userpwd',
            cls: 'pwd',
            blankText: '密码不能为空',
            regex: /^[\da-zA-Z_]{1,20}$/,
            regexText: "请填写1～20个数字或字母",
            fieldLabel: '密码',
            validateOnBlur: false,
            allowBlank: false
        }, checkcode
        ],
        buttons: [{
            text: '登录',
            cls: "x-btn-text-icon",
            icon: "/css/Images/icons/lock_open.png",
            height: 30,
            id: 'btnLogin',
            handler: function() {
             Ext.getCmp('loginForm').down('#btnLogin').setDisabled(true);
                if (loginForm.form.isValid()) {
                    if (!IsLogining) {
                        IsLogining = true;
                        Ext.getCmp('loginForm').down('#btnLogin').setDisabled(true);
                        loginForm.form.submit({
                            url: '/manager/user/login',
                            success: function(form, action) {
                                var flag = action.result.success;
                                if (flag == "true") {
                                    Ext.Msg.show({
                                        title: '提示',
                                        msg: '登录成功，正在跳转页面。。。',
                                        width: 300,
                                        icon: Ext.window.MessageBox.INFO
                                    });
                                    window.location.href = '/manager/home/index';
                                }
                                else {
                                    this.disabled = false;
                                   Ext.getCmp('loginForm').down('#btnLogin').setDisabled(false);
                                    IsLogining = false;
                                    console.log(action);
                                    var message = action.result.message;
                                    if (message) {
                                        Ext.MessageBox.alert("登录失败!", message);
                                    }
                                    else {
                                        Ext.MessageBox.alert("提示!", "登录失败");

                                    }
                                }

                            },
                            // 特别注意对于form.submit提交触发failure的条件是返回的success=false,和ajax.request是不一样的
                            failure: function(form, action) {
                                Ext.MessageBox.alert("提示!", action.result.msg);
                                IsLogining = false;
                            }
                        });
                    }
                }
            }
        }, {
            text: '重置',
            cls: "x-btn-text-icon",
            icon: "/css/Images/icons/arrow_redo.png",
            height: 30,
            handler: function() {
                loginForm.form.reset();
            }

}]
        });






    var win = new Ext.Window({
        title: '登录窗口',
        id: "loginWidow",
        iconCls: 'locked',
        width: 514,
        height: 344,
        resizable: false,
        draggable: true,
        modal: false,
        closable: false,
        layout: 'border',
        bodyStyle: 'padding:5px;',
        plain: false,
        items: [logoPanel, loginForm],
        buttonAlign: 'center'
            ,
        bbar: Ext.create('Ext.ux.StatusBar', {
            defaultText: '建议使用Google、火狐浏览器运行本程序',
            id: 'status-id',
            statusAlign: 'top'
            //                items: [{
            //                    text: 'A Button'
            //                }, '-', 'Plain Text']
        })
      
        });

        win.show();
        console.log("win.show90");
    };
    Ext.onReady(function() {
        Main_Login();
       
    });
